﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Permission.Interfaces;
using Microhard.WarehouseInsp.Buz.DataAccess;
using Microhard.WarehouseInsp.Buz.BuzEnum;

namespace Microhard.WarehouseInsp.Buz.Impls
{
    public class ProjectMngScopeImpl : ICustomeDataPermissionValidator
    {
        public bool HasPermission(List<Guid> identity)
        {
            WarehouseInspEntities db = new WarehouseInspEntities();
            //如果是风控专员或数据中心专员登录
            IEnumerable<SupProject> entities = (from b in db.SupProjects
                                                where (b.CreatorID == this.scope.CurrentUserID
                                                || b.PMID == this.scope.CurrentUserID)
                                                select b).ToList();
            //如果是监管员登录
            if (scope.CurrentUserRoleNames.Contains(SysConst.SUPOTALROLENAME))
            {
                entities = from b in db.PledgePlanInRecords
                           where b.Warehouse.WarehouseMngID == this.scope.CurrentUserID
                           select b.SupProject;
            }
            else if (scope.CurrentUserRoleNames.Contains(SysConst.PROTALROLENAME))
            {
                //如果是巡查员
                entities = from b in db.SupProjects
                           where b.ProtalEmpID == this.scope.CurrentUserID
                           select b;
            }

            foreach (var i in identity)
            {
                var rnt = entities.Select(p => p.ProjectID).Contains(i);
                if (rnt == false)
                    return false;
            }
            return true;
        }

        public Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }
    }
}
